package com.qqtech.qquan.user.service;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.frame.dao.BaseDao;
import com.qqtech.core.frame.service.BaseServiceImpl;
import com.qqtech.qquan.user.dao.UserFriendDao;
import com.qqtech.qquan.user.enums.UserFriendStatusDictEnum;
import com.qqtech.qquan.user.model.UserFriend;
import com.qqtech.qquan.user.model.UserFriendQuery;
import com.qqtech.qquan.user.model.UserFriendVo;

@Service
public class UserFriendServiceImpl extends BaseServiceImpl<UserFriend>
		implements UserFriendService {
	public static final Logger log = LoggerFactory.getLogger(UserFriendServiceImpl.class);
	
	@Resource
	private UserFriendDao userFriendDao;

	@Override
	protected BaseDao<UserFriend> getBaseDao() {
		return userFriendDao;
	}

	@Override
	public boolean isFriend(Integer userId1, Integer userId2) {
		return userFriendDao.isFriend(userId1, userId2);
	}

	@Override
	public List<Integer> queryMyFriendIds(Integer userId) {
		return userFriendDao.queryMyFriendIds(userId);
	}
	
	@Override
	public UserFriendVo queryNormalFriend(Integer userId1, Integer userId2){
		return userFriendDao.queryNormalFriend(userId1, userId2);
	}
	
	@Override
	public UserFriendVo queryFriendRecord(Integer userId1, Integer userId2){
		return userFriendDao.queryFriendRecord(userId1, userId2);
	}
	
	@Override
	public String getFriendRemark(Integer myUserId, Integer friendUserId){
		if(myUserId == null || friendUserId == null){
			return "";
		}
		UserFriendVo friend = this.queryNormalFriend(myUserId, friendUserId);
		if(friend != null){
			if(myUserId.intValue() == friend.getUserId1().intValue()){
				return StringUtil.isBlank(friend.getRemark1())? "" : friend.getRemark1();
			}else{
				return StringUtil.isBlank(friend.getRemark2())? "" : friend.getRemark2();
			}
		}else{
			return "";
		}
	}
	
	@Override
	public int queryFriendCount(Integer userId) {
		UserFriendQuery friendQ = new UserFriendQuery();
		friendQ.setUserId(userId);
		friendQ.setStatus(UserFriendStatusDictEnum.NORMAL.getCode());
		return userFriendDao.queryCount(friendQ);
	}
	
	@Override
	public boolean isFirst2AddFriend(Integer userId) {
		UserFriendQuery friendQ = new UserFriendQuery();
		friendQ.setUserId(userId);
		return userFriendDao.queryCount(friendQ) == 0;
	}

	@Override
	public int removeFriendShip(Integer userId1, Integer userId2) {
		return userFriendDao.removeFriendShip(userId1, userId2);
	}

	@Override
	public int recoveryFriendShip(Integer userId1, Integer userId2) {
		return userFriendDao.recoveryFriendShip(userId1, userId2);
	}
}